package com.example.demo.loginmanage.controller;

import com.example.demo.loginmanage.service.LoginService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value="/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> goIndex(@RequestBody Map<String,Object> param, HttpServletRequest request) throws SQLException {
        Map<String,Object> statusMap = loginService.queryUserInfosByUsername(param,request);
        return statusMap;
    }

    /**
         * @Description 初始化页面，查询所有中心店面
         * @Author zhangxue
         * @Date 8:53 2018/6/3
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/initPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(param.size()>=1){
            List<Map<String,Object>> resultList = loginService.queryAllSaleStore(param);
            resultMap.put("data",resultList);
            resultMap.put("status","1");
            //查询人员权限
            Map<String,Object> limMap = loginService.queryLimByUserNo(param);
            if(limMap==null){
                resultMap.put("lim","-1");
            }else{
                String lim = limMap.get("Lim")!=null?limMap.get("Lim").toString():"-1";
                resultMap.put("lim",lim);
            }
        }else{
            resultMap.put("status","0");
        }
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }


    /**
     * @Description 初始化sh_search.html页面，查询所有中心店面
     * @Author zhangxue
     * @Date 8:53 2018/6/3
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/initSearchPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initSearchPage( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(param.size()>=1){
            List<Map<String,Object>> resultList1 = loginService.queryAllSaleStore(param);
            resultMap.put("data",resultList1);
            List<Map<String,Object>> resultList2 = loginService.queryAllBrand(param);
            resultMap.put("data2",resultList2);
            resultMap.put("status","1");
            //查询人员权限
            Map<String,Object> limMap = loginService.queryLimByUserNo(param);
            if(limMap==null){
                resultMap.put("lim","-1");
            }else{
                String lim = limMap.get("Lim")!=null?limMap.get("Lim").toString():"-1";
                resultMap.put("lim",lim);
            }
        }else{
            resultMap.put("status","0");
        }
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

    /**
     * @Description 初始化授权页面，查询所有中心店面
     * @Author zhangxue
     * @Date 8:53 2018/6/3
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/initSqPage",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initSqPage( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        if(param.size()>=1){
            List<Map<String,Object>> resultList = loginService.queryAllSqStore(param);
            resultMap.put("data",resultList);
            resultMap.put("status","1");
            //查询人员权限
            Map<String,Object> limMap = loginService.queryLimByUserNo(param);
            if(limMap==null){
                resultMap.put("lim","-1");
            }else{
                String lim = limMap.get("Lim")!=null?limMap.get("Lim").toString():"-1";
                resultMap.put("lim",lim);
            }
        }else{
            resultMap.put("status","0");
        }
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

    /**
         * @Description 通过中心店面查询所有部门
         * @Author zhangxue
         * @Date 11:02 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllBmByStore",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllBmByStore( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        List<Map<String,Object>> resultList = loginService.queryAllBmByStore(param);
        resultMap.put("data",resultList);
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

    /**
     * @Description 通过部门查询所有员工
     * @Author zhangxue
     * @Date 11:02 2018/8/30
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/queryAllUserByBm",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryAllUserByBm( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        List<Map<String,Object>> resultList = loginService.queryAllUserByBm(param);
        resultMap.put("data",resultList);
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

    /**
         * @Description 授权
         * @Author zhangxue
         * @Date 14:49 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/shouquan",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String shouquan( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap = loginService.shouquan(param);
        js = JSONObject.fromObject(resultMap);
        return js.toString();
    }

    /**
         * @Description 根据员工编号查看权限
         * @Author zhangxue
         * @Date 15:41 2018/8/30
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/getLimByUserNo",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getLimByUserNo( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap = loginService.getLimByUserNo(param);
        js = JSONObject.fromObject(resultMap);

        return js.toString();
    }

    /**
     * @Description 删除授权
     * @Author zhangxue
     * @Date 14:49 2018/8/30
     * @Param
     * @retrun
     **/
    @RequestMapping(value="/delLim",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delLim( @RequestBody Map<String,Object> param){
        JSONObject js= new JSONObject();
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap = loginService.delLim(param);
        js = JSONObject.fromObject(resultMap);
        return js.toString();
    }

    /**
         * @Description 查询所有员工权限列表
         * @Author zhangxue
         * @Date 17:16 2018/10/1
         * @Param
         * @retrun
         **/
    @RequestMapping(value="/queryAllLimInfos",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Map<String,Object>> queryAllLimInfos( @RequestBody Map<String,Object> param){
        List<Map<String,Object>> resultList = new ArrayList<Map<String, Object>>();
        resultList = loginService.queryAllLimInfos(param);
        return resultList;
    }

}
